package sumoTween.demo.swing.scroll;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;

import sumoTween.Tweener;
import sumoTween.easing.Bounce;
import sumoTween.easing.Cubic;

public class DemoComponent extends JComponent 
{
	Tweener testTween=new Tweener();

	boolean mousedown=false;
	Box boxes[]=new Box[75];

	int scrollx=0;
	int scrolly=0;

	int mousex=0;
	int mousey=0;
	
	int mouseDiffx=0;
	int mouseDiffy=0;
	
	public DemoComponent()
	{
		testTween=new Tweener(new sumoTween.easing.Circ());

		this.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) 
			{
				testTween.clear();
				testTween.add(0, 1f, Tweener.EASING_OUT);
				testTween.add(1, 0f);
				mousedown=false;
			}
			
			public void mousePressed(MouseEvent e) 
			{
				mousedown=true;
			}
			public void mouseExited(MouseEvent e) {	}
			public void mouseEntered(MouseEvent e) {}
			public void mouseClicked(MouseEvent e) {}
		});
		
		this.addMouseMotionListener(new MouseMotionListener() {
			
			public void mouseMoved(MouseEvent e) {
				mousex=e.getX();
				mousey=e.getY();
			}
			
			public void mouseDragged(MouseEvent e) 
			{
				mouseDiffx=mousex-e.getX();
				mouseDiffy=mousey-e.getY();
				
				scrollx-=mouseDiffx;
				scrolly-=mouseDiffy;
				
				mousex=e.getX();
				mousey=e.getY();
				
				if(mouseDiffx>50)mouseDiffx=50;
				if(mouseDiffy>50)mouseDiffy=50;
				if(mouseDiffx<-50)mouseDiffx=-50;
				if(mouseDiffy<-50)mouseDiffy=-50;
			}
		});

		for(int i=0;i<boxes.length;i++)
		{
			boxes[i]=new Box();
		}

	}
	
	public void paint(Graphics g)
	{
		
		if(scrollx>1500)scrollx=1500;
		if(scrollx<-1000)scrollx=-1000;
		if(scrolly>1000)scrolly=1000;
		if(scrolly<-800)scrolly=-800;
		
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, getWidth(),getHeight());
		
		scrollx-=testTween.getValue()*mouseDiffx;
		scrolly-=testTween.getValue()*mouseDiffy;
		
		g.translate(scrollx, scrolly);
		
		for(int i=0;i<boxes.length;i++)
		{
			boxes[i].draw(g);
		}

		g.translate(-scrollx, -scrolly);
		
		if(mousedown)
		{
			g.setColor(Color.red);
			g.fillOval(mousex-20, mousey-20,40,40);
		}
	
	}
}
